package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.BaseStudentDao;
import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.until.Factory;

public class StudentService {
    private BaseStudentDao studentDao = Factory.getStudentDao();

    public Student[] findAllStudent() {
        Student[] stus = studentDao.findAllStudents();
        boolean flag = true;
        for (int i = 0; i < stus.length; i++) {
            Student s = stus[i];
            if (s != null) {
                flag = false;
            }
        }
        if (flag) {
            return null;
        } else {
            return stus;
        }
    }

    public boolean addStudent(Student stu) {
        return studentDao.addStudent(stu);
    }

    public boolean isExists(String id) {
        Student[] stus = studentDao.findAllStudents();
        boolean flag = false;
        for (int i = 0; i < stus.length; i++) {
            Student s = stus[i];
            if (s != null && s.getId().equals(id)) {
                flag = true;
            }
        }
        return flag;
    }

    public void deleteStudentById(String id) {
        studentDao.deleteStudentById(id);
    }

    public void updateStudentById(Student stu) {
        studentDao.updateStudentById(stu);
    }
}
